package com.hjm.project.report.report.controller;

import com.hjm.common.utils.Utils;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.framework.web.page.TableDataInfo;
import com.hjm.project.report.report.domain.ReportMerchant;
import com.hjm.project.report.report.service.ReportMerchantService;
import com.hjm.project.tBankinfo.tBankinfo.domain.TBankinfo;
import com.hjm.project.tBankinfo.tBankinfo.service.ITBankinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author puyiliang
 * @date create in 2021/1/16 15:39
 */
@Controller
@RequestMapping("/ReportMerchant")
public class ReportMerchantController extends BaseController {

    private String prefix = "ReportMerchant";
    @Autowired
    private ITBankinfoService tBankinfoService;
    @Autowired
    private ReportMerchantService reportMerchantService;

    /**
     * 商编查询页面
     * @return String
     */
    @GetMapping("/queryMerchant")
    public String queryMerchant(){
        return prefix + "/queryMerchant";
    }


    /**
     * 商编查询列表
     * @param request TableDataInfo
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:queryMerchantList")
    @PostMapping("/queryMerchantList")
    @ResponseBody
    public TableDataInfo queryMerchantList(@RequestParam Map<String, String> request)
    {
        startPage();
        List<ReportMerchant> list = reportMerchantService.selectQueryMerchantList(request);
        return getDataTable(list);
    }


    /**
     * 后台队列列表
     * @param request Map
     * @return TableDataInfo
     */
    @RequiresPermissions("ReportMerchant:getJobTaskList")
    @PostMapping("/getJobTaskList")
    @ResponseBody
    public TableDataInfo getJobTaskList(@RequestParam Map<String, String> request)
    {
        startPage();
        request.put("operatorType", "3");
        List<Map<String, String>> list = reportMerchantService.getJobTaskList(request);
        return getDataTable(list);
    }

    /**
     * 正式商编页面
     * @return String
     */
    @GetMapping("/formalMerchant")
    public String formalMerchant(){
        return prefix + "/formalMerchant";
    }

    /**
     * 后台队列页面
     * @return String
     */
    @GetMapping("/jobTask")
    public String jobTask(){
        return prefix + "/jobTask";
    }


    /**
     * 商编导入页面
     * @return String
     */
    @RequiresPermissions("ReportMerchant:uploadMerchant")
    @RequestMapping("/uploadMerchant")
    public String uploadMerchant(ModelMap map, String source){
        map.put("source", source);
        return prefix + "/uploadMerchant";
    }

    /**
     * 商编导入
     * @param file  MultipartFile
     * @param source source
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:uploadMerchant")
    @PostMapping("/uploadMerchant")
    public @ResponseBody Map saveUploadMerchant(@RequestParam("file") MultipartFile file, @RequestParam("source") String source){
        try {
            //文件复制
            String fileUrl = Utils.fileCopy(file);
            //文件导入
            reportMerchantService.saveUploadMerchant(fileUrl, this.getSysUser().getLoginName(), source);
            return success("已经生成新商编导入任务，稍后请前往任务队列查看，请耐心等待");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success("操作成功");
    }


    /**
     * 商编导出任务
     * @param request Map
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:downloadMerchant")
    @PostMapping("/downloadMerchant")
    public @ResponseBody Map downloadMerchant(@RequestParam Map<String, String> request){
        try {
            reportMerchantService.downloadMerchant(this.getSysUser().getLoginName(), request);
            return success("已经生成导出任务，稍后请前往任务队列查看，请耐心等待");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success("操作成功");
    }

    /**
     * 文件下载
     * @param id String
     * @param response HttpServletResponse
     */
    @RequiresPermissions("ReportMerchant:downloadFile")
    @GetMapping("/downloadFile")
    public void downloadFile(String id,  HttpServletResponse response){
        reportMerchantService.downloadFile(id, response);
    }

    /**
     * 商编导入页面
     * @return String
     */
    @RequiresPermissions("ReportMerchant:editMerchant")
    @RequestMapping("/editMerchant")
    public String editMerchant(){
        return prefix + "/editMerchant";
    }

    /**
     * 商编更新
     * @param file MultipartFile
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:editMerchant")
    @PostMapping("/editMerchant")
    public @ResponseBody Map saveEditMerchant(@RequestParam("file") MultipartFile file){
        try {
            String fileUrl = Utils.fileCopy(file);
            //文件导入
            reportMerchantService.saveEditMerchant(fileUrl, this.getSysUser().getLoginName());
            return success("已经生成新商编更新任务，稍后请前往任务队列查看，请耐心等待");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success("操作成功");
    }

    /**
     * 交易详情
     * @return String
     */
    @RequiresPermissions("ReportMerchant:transactionDetail")
    @RequestMapping("/transactionDetail")
    public String transactionDetail(ModelMap map, String bno){
        map.put("bno", bno);
        return prefix + "/transactionDetail";
    }

    /**
     * 交易详情查询列表
     * @param request TableDataInfo
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:transactionDetail")
    @PostMapping("/getTransactionDetailList")
    @ResponseBody
    public TableDataInfo getTransactionDetailList(@RequestParam Map<String, String> request)
    {
        startPage();
        List<Map<String, Object>> list = reportMerchantService.getTransactionDetailList(request);
        return getDataTable(list);
    }

    /**
     * 商编交易导出任务
     * @param request Map
     * @return Map
     */
    @RequiresPermissions("ReportMerchant:transactionDetail")
    @PostMapping("/downloadTransactionDetail")
    public @ResponseBody Map downloadTransactionDetail(@RequestParam Map<String, String> request){
        try {
            reportMerchantService.downloadTransactionDetail(this.getSysUser().getLoginName(), request);
            return success("已经生成导出任务，稍后请前往任务队列查看，请耐心等待");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success("操作成功");
    }
    /**
     * 修改商编
     */
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") String tid, ModelMap mmap)
    {
       //TBankinfo tBankinfo = tBankinfoService.selectTBankinfoById(tid);
        TBankinfo tBankinfo = new TBankinfo();
        tBankinfo.setTid(tid);
        List<TBankinfo> tBankinfos = tBankinfoService.selectTBankinfoList(tBankinfo);
        mmap.put("tBankinfo", tBankinfos.get(0));
        return prefix + "/edit";
    }

    /**
     * 修改保存商编
     */
    @RequiresPermissions("ReportMerchant:ReportMerchant:edit")
    @Log(title = "商编", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TBankinfo tBankinfo)
    {
        return toAjax(tBankinfoService.updateTBankinfo(tBankinfo));
    }
}
